package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseStrJedisProxy;

/**
 * 并发数Redis操作代理
 * 
 * @author Davey.wu
 */
public class ConcurrencyNumberJedisProxy extends BaseStrJedisProxy<Double> {
	/** 时间格式，精确到秒 **/
	private static final SimpleDateFormat DATEFORMATE = new SimpleDateFormat(
			Constants4Redis.DATE_PATTERN_YYYYMMDDHHMMSS_NOSPACE);

	public ConcurrencyNumberJedisProxy() {
		this.setTimeOut(Constants4Redis.REDIS_TIMEOUT_CONCURRENCY_NUMBER);
		this.setPrefix(Constants4Redis.PREFIX_CONCURRENCY_NUMBER);
	}

	@Override
	public long setex(String key, Double value) throws JedisException,
			JsonProcessingException {
		if (value == null) {
			return 0l;
		}
		double result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0;
		}
		boolean broken = false;
		try {
			result = jedis.incrByFloat(key, value);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}

		if (result > 0) {
			return 1l;
		}
		return 0;
	}

	public boolean setex(String key1, Double value1, String key2, Double value2)
			throws JedisException, JsonProcessingException {
		if (value1 == null) {
			value1 = 0.0;
		}
		if (value2 == null) {
			value2 = 0.0;
		}
		boolean result = false;
		// 获取REDIS连接，不尝试初始化参数
		Jedis jedis = JedisPoolUtils.getJedis(false);
		if (jedis == null) {
			return false;
		}
		boolean broken = false;
		try {
			Pipeline pipe = jedis.pipelined();
			pipe.incrByFloat(key1, value1);
			pipe.incrByFloat(key2, value2);
			pipe.sync();
			result = true;
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	@Override
	public Double get(String key) throws JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0.0;
		}
		boolean broken = false;
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		try {
			return Double.parseDouble(result);
		} catch (Exception e) {
			return 0.0;
		}
	}

	@Override
	public String getKey(String field) {
		return this.getPrefix() + KEY_SEPARATOR + field;
	}

	public String generateKey(Date date) {
		return this.getPrefix() + KEY_SEPARATOR + getSection(date);
	}

	@Override
	public String getKeyFromObject(Double value) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 获取时间区间
	 * 
	 * @param date
	 *            ：日期
	 * @return
	 */
	public static String getSection(Date date) {
		return DATEFORMATE.format(date);
	}
}
